package com.lantosec.hmrp.modules.warning.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.warning.entity.WarningData;
import com.lantosec.hmrp.modules.warning.entity.WarningEvalNote;
import com.lantosec.hmrp.modules.warning.entity.WarningSummary;
import com.lantosec.hmrp.modules.warning.service.IWarningDataService;
import com.lantosec.hmrp.modules.warning.vo.WarningDataVO;
import com.lantosec.hmrp.modules.warning.vo.WarningSummaryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预警数据 控制器
 *
 * @author AutoCode
 * @since 2020-05-27
 */
@Api(value = "预警数据控制器", tags = "预警数据控制器")
@RestController
@RequestMapping("/warning/data")
@Log4j2
public class WarningDataController extends BaseController {
    @Autowired
    private IWarningDataService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<WarningDataVO>> all() {
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("summaryStatistics")
    @ApiOperation(value = "汇总统计")
    public Result<Page<WarningSummaryVO>> summaryStatistics(
            @ApiParam(value = "分页对象", required = true) PageVO<WarningSummary> page, WarningSummaryVO condition,
            @ApiParam(value = "权限分类，1个人2科室3全院") @RequestParam(name = "permissionsType", required = false) Integer permissionsType) {
        Page<WarningSummary> warningSummaryPage = service.summaryStatistics(page, condition, permissionsType);
        return Result.ok(entity2VO(warningSummaryPage));
    }

    @GetMapping("notHandle")
    @ApiOperation(value = "未处置")
    public Result<Page<WarningDataVO>> notHandle(@ApiParam(value = "分页对象", required = true) PageVO<WarningData> page, String ruleId) {
        Page<WarningData> notHandle = service.notHandle(page, ruleId);
        return Result.ok(entity2VO(notHandle));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<WarningDataVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name = "id") String id) {
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("handle")
    @ControllerLog(name = "预警处置", type = LogType.update)
    @ApiOperation(value = "处置")
    public Result<Integer> create(
            @ApiParam(value = "处置对象", required = true) @RequestBody @Validated List<WarningEvalNote> list, BindingResult bindingResult) {
        validationHandler(bindingResult);
        list.get(0).setHandleUser(getName());
        list.get(0).setHandleUserId(getUserId());
        service.create(list);
        return Result.ok();
    }

    @GetMapping("hasDisposal")
    @ApiOperation(value = "已处置")
    public Result<Page<WarningDataVO>> hasDisposal(
            @ApiParam(value = "分页对象") PageVO<WarningData> page,
            @ApiParam(value = "预警ID") @RequestParam(name = "ruleId", required = false) String ruleId,
            @ApiParam(value = "区分pc端跟app,null为pc") @RequestParam(name = "flag", required = false) Integer warningFlag) {
        Page<WarningData> param = service.hasDisposal(page, ruleId, warningFlag);
        return Result.ok(entity2VO(param));
    }

    @GetMapping("disposalRejected")
    @ApiOperation(value = "处置驳回")
    public Result<Page<WarningDataVO>> disposalRejected(
            @ApiParam(value = "分页对象", required = true) PageVO<WarningData> page, String ruleId) {
        Page<WarningData> param = service.disposalRejected(page, ruleId);
        return Result.ok(entity2VO(param));
    }

    @GetMapping("pending")
    @ApiOperation(value = "待审核")
    public Result<Page<WarningDataVO>> pending(
            @ApiParam(value = "分页对象", required = true) PageVO<WarningData> page, String ruleId) {
        Page<WarningData> param = service.pending(page, ruleId);
        return Result.ok(entity2VO(param));
    }

    @PostMapping("audit")
    @ControllerLog(name = "三级预警处置完成审核", type = LogType.update)
    @ApiOperation(value = "审核")
    public Result<Integer> audit(@RequestBody WarningData data) {
        Integer dataId = service.audit(data);
        return Result.ok(dataId);
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "预警数据VO对象", required = true) @RequestBody WarningDataVO vo) {
        WarningData entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }
}
